# https://leetcode.cn/problems/diameter-of-binary-tree/description/

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def __init__(self):
        self.res = None

    def diameterOfBinaryTree(self, root: TreeNode) -> int:
        self.res = 1

        def deep(node: TreeNode) -> int:
            if node is None:
                return 0
            leftNode = deep(node.left)
            rightNode = deep(node.right)
            self.res = max(self.res, leftNode + rightNode + 1)
            return max(leftNode, rightNode) + 1

        deep(root)
        return self.res - 1
